package com.neusoft.modules.web.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neusoft.modules.web.dao.DeviceDao;
import com.neusoft.modules.web.entity.DeviceEntity;
import com.neusoft.modules.web.service.DeviceService;
import org.springframework.cache.annotation.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


/**
 * 设备管理
 */
@Service("deviceService")
@CacheConfig(cacheNames = "device")
public class DeviceServiceImpl extends ServiceImpl<DeviceDao, DeviceEntity> implements DeviceService {

	@Resource
	private DeviceDao deviceDao;

	@Override
	@Transactional
	@Caching(
			evict = {
					@CacheEvict(value = "selectAll", key = "'all'")
			},
			put = {
					@CachePut(key = "#device.id")
			}
	)
	public DeviceEntity saveDevice(DeviceEntity device) {
		this.save(device);
		return device;
	}

	@Override
	@Transactional
	@Caching(
			evict = {
					@CacheEvict(value = "selectAll",key = "'all'")
			},
			put = {
					@CachePut(key = "#device.id")
			}
	)
	public DeviceEntity updateDevice(DeviceEntity device) {
		this.updateById(device);
		return device;
	}

	@Override
	@Caching(
			evict = {
					@CacheEvict(value = "selectAll", key = "'all'"),
					@CacheEvict(key = "#id")
			}
	)
	public void deleteDevice(Long id) {
		this.removeById(id);
	}

	@Override
	@Cacheable(value = "selectAll",key = "'all'")
	public List<DeviceEntity> queryAllDevices() {
		return deviceDao.queryAllDevices();
	}

	@Override
	@Cacheable(key = "#id")
	public DeviceEntity queryDeviceById(Long id) {
		DeviceEntity device = this.getById(id);
		return device;
	}
}